package org.example;

import org.example.cassell_client.service.BulletinServiceClient;
import org.example.cassell_client.service.DeptServiceClient;
import org.example.cassell_client.service.UserServiceClient;
import org.example.util.JWTUtil;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.PromptChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.time.Duration;
import java.time.LocalDate;
import java.util.Map;


@RestController
@RequestMapping("/eva")
@EnableFeignClients(basePackages = "org.example.cassell_client.service")
public class evaController {
    private  final ChatClient chatClient;
    @Autowired
    private BulletinServiceClient bulettinbServiceClient;
    @Autowired
    private UserServiceClient userServiceClient;
    @Autowired
    private DeptServiceClient deptServiceClient;
    public evaController(ChatClient.Builder chatClient, ChatMemory chatMemory) {
        this.chatClient=chatClient.defaultSystem(
                "你是dook学院论坛的智能机器人，与你对话的是论坛的线上用户，你将要回答他们的问题，请务必带入角色" +
                        "请讲中文" +
                        "在提供修改密码服务之前，您必须从用户处获取如下信息：需要修改的密码。"+
                        "请调用自定义函数执行修改密码,该函数的所需的Email，即用户的账号是{user},不可以向用户获取" +
                        "如果用户没有提供修改的密码，那么就返回用户没有提供修改的密码" +
                        "请调用自定义函数执行获取用户列表" +
                        "请调用自定义函数获取nacos状态,您必须从用户处获取如下信息：需要获取的nacos服务名，并根据当前服务的健康状态和元数据提供分析" +
                        "请调用自定义函数执行获取公告列表,简略总结分析给用户,要求主要要返回序号（id）给用户，并根据用户需求提供公告数据" +
                        "请调用自定义函数执行获取公告内容,您必须从用户处获取如下信息：需要获取的公告内容的内容id(contentId)，并将其中内容返回给用户" +
                        "今天的日期是{current_date}" +
                        "当前用户的账号是{user}"+
                        "如果当前用户的账号是dc，那么他拥有管理权限"
                )
                .defaultAdvisors(new PromptChatMemoryAdvisor(chatMemory))
                .build();
    }
    @GetMapping(value = "/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> chat(@RequestParam(name="message") String message,@RequestParam(name="token")String  token) {
        Map<String, Object> c= JWTUtil.parseToken(token);
        String Email=(String)(c.get("Email"));
        return this.chatClient.prompt()
                .user(message)
                .functions("updatePassWord","userlist","getNacos","bulletinlist")
                .system(promptSystemSpec -> promptSystemSpec.param("user",Email))
                .system(promptSystemSpec -> promptSystemSpec.param("current_date", LocalDate.now().toString()))
                .advisors(advisorSpec -> advisorSpec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY, 20))
                .stream()
                .content()
                .concatWithValues("[complete]") // 使用concatWithValues替代concatWith
                .delayElements(Duration.ofMillis(50)); // 添加微小延迟防止消息合并
    }
}
